package com.banwoqiche.utils;
import com.banwoqiche.dto.MyBackCode;
import com.banwoqiche.exceptions.MyBaseException;
import com.banwoqiche.pojo.SysUser;
import com.banwoqiche.pojo.MySecurityUser;
import org.springframework.beans.BeanUtils;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @time: 2022-03-31 23:53
 */
public class SecurityUtils {
 public static SysUser getLoginUser() {
  Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
  if (!(authentication instanceof AnonymousAuthenticationToken)) {
   SysUser backUser = new SysUser();
   MySecurityUser principal = (MySecurityUser) authentication.getPrincipal();
   BeanUtils.copyProperties(principal, backUser);
   return backUser;
  }else {
   throw new MyBaseException("--->>>ERROR:getLoginUser is null", MyBackCode.NOT_LOGIN);
  }
 }
}
